﻿/*=====================================================================
  
    File:      TabularDatabase.Database.cs

    Summary:   This is the class that implements all related functionality 
               around Database in a tabular model, for all TabularDatabase 
               commandlets.
               Functionality for children objects (like Tables, Relationships, 
               etc.) is implemented in separate classes.
 
               Tabular Database Cmdlets (TabularDatabaseCmdlets) is sample code 
               to show and explain how to use the AMO to Tabular (AMO2Tabular)
               library to build a PowerShell library of cmdlets to manage 
               Tabular model objects. 
               The sample can be seen as a sample library of cmdlets
               with the necessary code to execute each particular 
               action or operation over a logical tabular object. 

    Authors:   JuanPablo Jofre (jpjofre@microsoft.com)
    Date:	   04-Apr-2012
  
    Change history:

    @TODO: 
  
-----------------------------------------------------------------------
  
    This file is part of the Microsoft SQL Server Code Samples.
    Copyright (C) Microsoft Corporation.  All rights reserved.
  
  This source code is intended only as a supplement to Microsoft
  Development Tools and/or on-line documentation.  
  
  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF 
  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  PARTICULAR PURPOSE.
  
==================================================================== */

using System;
using System.Globalization;
using System.Management.Automation;     //  Windows PowerShell assembly.
using MicrosoftSql2012Samples.Amo2Tabular;
using MicrosoftSql2012Samples.TabularDatabaseCmdlet.Properties;
using AMO = Microsoft.AnalysisServices;

namespace MicrosoftSql2012Samples.TabularDatabaseCmdlet
{
    [Cmdlet(VerbsCommon.Add, "TabularDatabase")]
    public class AddTabularDatabase : TabularDatabaseBase
    {
        // Declare optional parameters for the Add-TabularDatabase cmdlet.
        [Parameter]
        public string datasourceOledbConnectionString { get; set; }

        [Parameter]
        public string connectionName { get; set; }

        [Parameter]
        public int lcid { get; set; }

        [Parameter]
        public string collationName { get; set; }

        // Overide the ProcessRecord method to add a Tabular Database
        // using the supplied server name and database name 
        protected override void ProcessRecord()
        {
            using (AMO.Server server = new AMO.Server())
            {
                try
                {
                    server.Connect(serverName);
                    AMO2Tabular.TabularDatabaseAdd(server, databaseName, datasourceOledbConnectionString, connectionName, lcid, collationName);
                    WriteVerbose(string.Format(CultureInfo.InvariantCulture, Resources.AddTabularDatabaseWriteReport, databaseName, serverName));
                }
                catch (Exception ex)
                {
                    ThrowTerminatingError(new ErrorRecord(ex, string.Format(CultureInfo.InvariantCulture, Resources.AddTabularDatabaseTerminatingError, databaseName, serverName), ErrorCategory.InvalidOperation, null));
                }
            }

        }

    }
}
